<?php
namespace app\service;

use app\exception\ErrnoPlus;
use app\model\SendTemplateRelationModel;
use share\exception\ApiException;
use share\service\BaseModelService;

class SendTemplateRelationService extends BaseModelService{

    CONST CLIENT_TYPE_OF_VENDOR = 1;
    CONST CLIENT_TYPE_OF_APP = 2;

    public static function getModel()
    {
        return new SendTemplateRelationModel();
    }

    public static function setRelation($params){
        self::delByFilter([
            'other_id' => $params['other_id'],
            'other_type' => $params['other_type'],
        ]);
        $sendTemplateIds = $params['send_template_id'];
        $tn = time();
        if(count($sendTemplateIds) > 0){
            $sendTemplate = SendTemplateService::getAllByIds($sendTemplateIds);
            $priority =  $params['other_type'] == self::CLIENT_TYPE_OF_VENDOR ? 1 : 2;
            $items = [];
            foreach($sendTemplate as $template){
                $items[] = [
                    'other_id' => $params['other_id'],
                    'other_type' => $params['other_type'],
                    'priority' => $priority,
                    'send_template_id' => $template['id'],
                    'created' => $tn,
                    'updated' => $tn,
                ];
            }
            self::addBulk($items);
        }   
    }
    
    public static function getBestMatchRelation($vendor,$package,$language,$type,$scene){
        $vendor = strtoupper(trim($vendor));        
        $vendorInfo = VendorService::getByFilter([
            'no' => $vendor,
        ]);
        if(empty($vendorInfo)){
            throw new ApiException("vendor not exist [{$vendor}]",ErrnoPlus::INVALID_PARAM);
        }
        $appInfo = AppInfoTableService::getByFilter([
            'package' => $package,
        ]);
        if($appInfo){
            // 如果有配置app的
            $relations = self::getAllByFilter([
                'other_id' => $appInfo['id'],
                'other_type' => self::CLIENT_TYPE_OF_APP,
            ]);
            if($relations){
                $sendTemplate = SendTemplateService::getSendTemplate(array_column($relations,'send_template_id'),$language,$type,$scene);
                if($sendTemplate){
                    return $sendTemplate;
                }
            }
        }
        // 如果有配置厂商的
        $relations = self::getAllByFilter([
            'other_id' => $vendorInfo['id'],
            'other_type' => self::CLIENT_TYPE_OF_VENDOR,
        ]);
        if($relations){
            $sendTemplate = SendTemplateService::getSendTemplate(array_column($relations,'send_template_id'),$language,$type,$scene);
            if($sendTemplate){
                return $sendTemplate;
            }
        }
        return SendTemplateService::getDefaultSendTemplate($language,$type,$scene);

    }

    public static function buildCondition($filter)
    {
        $res = [];
        if(isset($filter['app_id']) && !empty($filter['app_id'])){
            $res['other_id'] = $filter['app_id'];
            $res['other_type'] = self::CLIENT_TYPE_OF_APP;
        } 
        if(isset($filter['vendor_id']) && !empty($filter['vendor_id'])){
            $res['other_id'] = $filter['vendor_id'];
            $res['other_type'] = self::CLIENT_TYPE_OF_VENDOR;
        } 
        if(isset($filter['send_template_id']) && !empty($filter['send_template_id'])){
            $res['send_template_id'] = $filter['send_template_id'];
        }
        return $res;
    }
}